﻿
@{
    ViewBag.Title = "PersonSelector";
    Layout = "~/Views/Shared/_LayoutBasal.cshtml";
}

@Styles.Render("~/Content/zTreeStyle/zTreeStyle.css")
@Scripts.Render("~/Scripts/ztree/jquery.ztree.core-3.5.min.js")


<style type="text/css">
    .ztree li span.button.switch.level0 {
        visibility: hidden;
        width: 1px;
    }

    .ztree li ul.level0 {
        padding: 0;
        background: none;
    }
</style>

<script>

    var _treeObj;


    //当前页
    var _currentPage = 1;

    $(document).ready(function ()
    {
        loadOrganizationTree();
    });

    function loadOrganizationTree()
    {
        $.ajax({
            url: "/Api/Domain/GetOrganizationList?domainId=" + '@ViewBag.Domain.Id',
            type: "POST",
            dataType: "json",
            success: function (data, status, jqXHR)
            {
                if (data.Success)
                {
                    var setting = {
                        view: {
                            dblClickExpand: false,
                            showLine: true,
                            selectedMulti: false
                            //  fontCss: setFontCss
                        },
                        data: {
                            key: {
                                name: "Name"
                            },
                            simpleData: {
                                enable: true,
                                idKey: "Id",
                                pIdKey: "Parent",
                                rootPId: "@ViewBag.RootOrganization.Id"
                            }
                        },
                        callback: {
                            onClick: zTreeOnClick
                        }
                    };

                    var zNodes = new Array();

                    @*var root = new Object();
                    root.Id = '@ViewBag.Domain.Id';
                    root.Parent = '';
                    root.Name = '@ViewBag.Domain.Name';
                    zNodes[0] = root;*@

                    //var i = 1;
                    var i = 0;
                    $.each(data.Data, function (n, value)
                    {
                        var oranization = new Object();
                        oranization.Id = value.Id;
                        oranization.Parent = value.Parent;
                        oranization.Name = value.Name;
                        zNodes[i] = oranization;
                        i++;
                    });

                    _treeObj = $.fn.zTree.init($("#tree"), setting, zNodes);
                    _treeObj.expandAll(true);
                    var nodes = _treeObj.getNodes();
                    if (nodes.length > 0)
                    {
                        _treeObj.selectNode(nodes[0]);
                        selectNode(nodes[0]);
                    }

                } else
                {
                    layerAlert(data.Message);
                }
            },
            error: function (xmlHttpRequest)
            {
                alert("Error: " + xmlHttpRequest.status);
            }
        });
    }

    function zTreeOnClick(event, treeId, treeNode)
    {
        selectNode(treeNode);
    }

    function selectNode(treeNode)
    {
        //getUserList(treeNode.Id);
        getUserList();
    }

    function getUserList(targetPage)
    {
        var treeNode = getSelectedNode();
        if (treeNode == null)
            return;

        var organizationId = treeNode.Id;

        var args = new Object();
        args.Page = targetPage || 1;
        args.PageSize = 7;
        args.OrganizationId = organizationId;
        args.Name = $("#txtName").val();
        args.SearchOrganization = $("#selectSearchOrganization").val();

        var layerIndex = layer.load(0, {
            shade: [0.2, '#fff']
        });

        //http://www.jb51.net/article/48943.htm
        //$('#tableUser tr:gt(1)').remove();

        $.ajax({
            url: "/Api/User/GetUserList",
            type: "POST",
            dataType: "json",
            data: JSON.stringify(args),
            success: function (data, status, jqXHR)
            {
              //  alert(data);
                layer.close(layerIndex);
               
                if (data.Success)
                {
                    //$.each(data.Data.UserList, function (n, value)
                    //{
                    //    addUserRow(value);
                    //});

              //      alert(JSON.stringify(data));

                    var resultObj = data.Data;

                    _currentPage = resultObj.Page;

                    //alert(JSON.stringify(resultObj));
                    var gettpl = document.getElementById('tableTemplate').innerHTML;
                    laytpl(gettpl).render(resultObj.ItemList, function (html) {
                        document.getElementById('divTableBodyContainer').innerHTML = html;
                        fitTable();
                    });

                    laypage({
                        skin: 'yahei',
                        cont: document.getElementById('divPagingContainer'),
                        pages: resultObj.TotalPage, //总页数
                        curr: resultObj.Page, //当前页
                        groups: 7, //连续显示分页数
                        jump: function (obj, first) {
                            if (!first) { //点击跳页触发函数自身，并传递当前页：obj.curr
                                getUserList(obj.curr);
                            }
                        }
                    });

                } else
                {
                    layerAlert(data.Message);
                }
            },
            error: function (xmlHttpRequest)
            {
                layer.close(layerIndex);

                alert("Error: " + xmlHttpRequest.status);
            }
        });
    }

    function addUserRow(user)
    {
        var table = document.getElementById("tableUser");
        var newRow = table.insertRow();

        var tdNumber = newRow.insertCell();
        tdNumber.height = 35;
        tdNumber.innerHTML = user.Number;

        var tdName = newRow.insertCell();
        tdName.innerHTML = "<a href='javascript:void(0)' onclick=\"selectPerson('" + user.Id + "','" + user.Name + "')\">" + user.Name + "</a>";

    }

    function selectPerson(id, name)
    {
        parent.selectPerson(getQueryString("domId"), id, name);
    }

    function closePersonSelector()
    {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭   
    }

    function scrollHeader() {
        // alert(divTableBodyContainer.scrollLeft);
        var ml = 0 - divTableBodyContainer.scrollLeft;
        document.getElementById("tableHeader").style.cssText = "margin-left:" + ml + "px;";
    }

    function fitTable() {
        $("#tableBody").width($("#tableHeader").width());

        $("#tableHeader tr:first").each(function (n, value) {
            $(this).find("td").each(function (n, value) {
                $("#tableBody tr:first td:eq(" + n + ")").width(value.width)
            });
        });
    }

    function getSelectedNode() {
        var nodes = _treeObj.getSelectedNodes();
        if (nodes.length != 1)
            return null;

        return nodes[0];
    }

</script>

<script id="tableTemplate" type="text/html">

    <table id="tableBody" border="0" cellspacing="0" cellpadding="0">
        {{# for(var i = 0, len = d.length; i < len; i++){ }}

        <tr>
            <td height="35">{{ d[i].Account }}</td>
            <td><a href="javascript:void(0)" onclick="selectPerson('{{ d[i].Id }}','{{ d[i].Name }}')">{{ d[i].Name }}</a></td>
        </tr>

        {{# } }}

    </table>

</script>

<div style="margin:10px 20px;">
    <div style="float:left;line-height:30px;">
        <b>查询</b>
    </div>
    <div style="float:left;line-height:30px;margin-left:20px;">
        <span>姓名：</span>
        <input class="input_16" style="width:100px;" id="txtName" />
    </div>
    <div style="float:left;line-height:30px;margin-left:20px;">
        <span>组织机构：</span>
        <select class="input_16" style="width:170px;" id="selectSearchOrganization">
            <option value="0">当前选中及下级</option>
            <option value="1">仅当前选中</option>
        </select>
    </div>
    <div style="float:left; margin-left:20px;">
        <input name="btnCreateChildOrganization" type="button" class="btn_blue" id="btnCreateChildOrganization" value="查询" onclick="getUserList()" />
    </div>
    <div style="clear:both"></div>
</div>

<div style="margin:10px; background-color:#ccc; height:1px"></div>


<div style="margin:10px 20px;">
    <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
            <td>
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                        <td width="200" valign="top">
                            <div style="height:380px; overflow:auto">
                                <ul id="tree" class="ztree"></ul>
                            </div>
                        </td>
                        <td width="10" valign="top">&nbsp;</td>
                        <td valign="top">
                            @*<div style="height:300px; overflow:auto">
                                    <table width="100%" border="0" cellspacing="0" cellpadding="0" id="tableUser">
                                        <tr>
                                            <td width="150"><strong>工号</strong></td>
                                            <td width="190"><strong>姓名</strong></td>
                                        </tr>
                                        <tr>
                                            <td colspan="2" height="20"><div style="background:#D4D4D4; height:1px;"></div></td>
                                        </tr>
                                        <tr>
                                                <td height="30">015100</td>
                                                <td><a href="#">张三</a></td>
                                            </tr>
                                    </table>
                                </div>*@
                            <div style="overflow:auto; height:370px; ">
                                <div style="overflow:hidden; padding-left:20px;" class="tableHeader">
                                    <table id="tableHeader" border="0" cellspacing="0" cellpadding="0" width="340" height="47">
                                        <tr>
                                            <td width="150"><strong>工号</strong></td>
                                            <td width="190"><strong>姓名</strong></td>
                                        </tr>
                                    </table>
                                </div>
                                <div id="divTableBodyContainer" style="overflow:auto; padding-left:20px; " onscroll="scrollHeader()">
                                    <!--table必须要有一个确切的宽度，否则无法超出div-->
                                </div>
                                <div id="divPagingContainer" style="margin-top:20px; margin-bottom:20px;text-align:right; ">
                                </div>
                            </div>
                        </td>
                    </tr>
                </table>

               
            </td>
        </tr>
    </table>
</div>


<div style="position:absolute; bottom:15px; left:20px;right:20px;">


    @*<div style="float:left;">
        <input name="btnSave" type="button" class="btn_red" id="btnSave" value="删 除" />
    </div>*@

    <div style="float:right">
        <input type="button" class="btn_blue" value="取 消" style="margin-left:1px;" onclick="closePersonSelector()" />
    </div>
    <div style="clear:both">
    </div>
</div>